package com.gudaomai.wuye.web;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;

import java.util.List;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;


import com.baomidou.mybatisplus.toolkit.IdWorker;
import com.gudaomai.wuye.common.CommonConstant;
import com.gudaomai.wuye.common.CommonException;
import com.gudaomai.wuye.common.MsgConstant;
import com.gudaomai.wuye.entity.HouseOwner;
import com.gudaomai.wuye.entity.HouseOwnerInfo;
import com.gudaomai.wuye.entity.HouseOwnerInput;
import com.gudaomai.wuye.entity.RetEntity;
import com.gudaomai.wuye.service.IHouseOwnerServ;

import org.springframework.web.bind.annotation.RequestParam;


import com.gudaomai.wuye.web.BaseController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zenghualu
 * @since 2017-08-18
 */
@Api(description="业主信息操作")
@Controller
@RequestMapping("/wuye/houseOwner")
public class HouseOwnerCtrl extends BaseController {

	@Autowired
	IHouseOwnerServ houseOwnerService;
	/** 
	 * add a new HouseOwner
	 * 
	 * 1. Check if all required field are filled in.
	 * 2. Check if all the inputs are valid.
	 * 3. Check if HouseOwner with the same properties does not exist in the DB.
	 * 4. Save the inputed values.
	 * 
	 *@param  
	 *@author jess
	 *return 
	 *
	 **/
	@ApiOperation("The houseOwner settings page, click the add button, add houseOwner")
	@RequestMapping(value = "/", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public RetEntity addHouseOwner(@RequestBody HouseOwnerInput houseOwnerInput) throws Exception {
		
		RetEntity entity = new RetEntity();

		if(houseOwnerService.isExist(houseOwnerInput.getIdHouse())){
			throw new CommonException(CommonConstant.RESULT_CODE_CREATED, 
					getMessage(MsgConstant.RET_HOUSEOWNER_ADD_FAILURE));
		} 
		HouseOwner houseOwnerEntity = new HouseOwner();
		BeanUtils.copyProperties(houseOwnerInput, houseOwnerEntity);
		houseOwnerEntity.setId(IdWorker.get32UUID());
		
		if (houseOwnerService.insert(houseOwnerEntity)) {
			entity.setRetCode(CommonConstant.RESULT_CODE_OK);
			entity.setRetMessage(getMessage(MsgConstant.RET_HOUSEOWNER_ADD_SUCCESS));
		}else {
			throw new CommonException(CommonConstant.RESULT_CODE_CREATED, 
					getMessage(MsgConstant.RET_HOUSEOWNER_ADD_FAILURE));
		}	
		
		return entity;
		
	}
	
	/** 
	 * query houseOwner information
	 * 1. Check if the corresponding Id exists in the DB.
	 * 2. Read and return the result set from DB .
	 * 
	 *@param  id
	 *@author jess
	 *return 
	 *
	 **/
	@ApiOperation("The houseOwner settings page, click the query button, query houseOwner info")	
	@GetMapping("/id/{id}")
	public HouseOwner getHouseOwnerById(@PathVariable("id") String id) throws Exception{
		
		HouseOwner houseOwner = houseOwnerService.selectById(id);
		
		houseOwner.setRetCode(CommonConstant.RESULT_CODE_OK);
		houseOwner.setRetMessage(getMessage(MsgConstant.RET_HOUSEOWNER_SELECT_SUCCESS));
		return houseOwner;
	}
	
	/** 
	 * update the existing houseOwner information.
	 * 
	 * 1. Check if all required field are filled in.
	 * 2. Check if all the inputs are valid.
	 * 3. update houseOwner information in the DB.
	 * 
	 *@param 
	 *@author jess
	 *return 
	 *
	 **/
	@ApiOperation("The houseOwner settings page, click the modify button, update houseOwner")
	@RequestMapping(value = "/", method = RequestMethod.PUT)
	public RetEntity updateHouseOwner(@RequestBody HouseOwnerInput houseOwnerInput) throws Exception{
				
		
        RetEntity entity = new RetEntity();
		
		HouseOwner houseOwnerEntity = new HouseOwner();
		BeanUtils.copyProperties(houseOwnerInput, houseOwnerEntity);
		
		if (houseOwnerService.updateById(houseOwnerEntity)) {
			entity.setRetCode(CommonConstant.RESULT_CODE_OK);
			entity.setRetMessage(getMessage(MsgConstant.RET_HOUSEOWNER_UPDATE_SUCCESS));
		}else {
			throw new CommonException(CommonConstant.RESULT_CODE_NOT_IMPLEMENTED,
					getMessage(MsgConstant.RET_HOUSEOWNER_UPDATE_FAILURE));
		}
		houseOwnerService.updateById(houseOwnerEntity);
		
		//Return message and HTTP result code
		entity.setRetCode(CommonConstant.RESULT_CODE_OK);
		entity.setRetMessage(getMessage(MsgConstant.RET_HOUSEOWNER_UPDATE_SUCCESS));
		
		return entity;
	}
	
	
	/** 
	 * delete the existing houseOwner
	 * 1. Check if the corresponding Id exists in the DB.
	 * 2. Delete the houseOwner information from the DB.
	 * 
	 *@param  id
	 *@author jess
	 *return 
	 *
	 **/
	@ApiOperation("The HouseOwner settings page, click the delete button, delete HouseOwner")
	@RequestMapping(value="/id/{id}",method=RequestMethod.DELETE)
	public RetEntity delHouseOwnerById(@PathVariable("id") String id) throws Exception{
		
        RetEntity entity = new RetEntity();
		
		houseOwnerService.deleteById(id);
		
		entity.setRetCode(CommonConstant.RESULT_CODE_OK);
		entity.setRetMessage(getMessage(MsgConstant.RET_HOUSEOWNER_DELETE_SUCCESS));
		return entity;
	}

	/**
	 * 根据住房编号或者业主信息获取房主信息
	 * 1.数据完整性检查--house表中有没有houseId
	 * 2.查询住房的详细地址，格式为楼号-单元号-房号
	 * 3.同时获取业主的车位信息
	 * @param houseId
	 * @param ownerInfo
	 * @return
	 * @throws CommonException 
	 */
	@ApiOperation("信息（退款，调整申请）添加页面，根据住房编号或者业主信息获取房主信息")
	@RequestMapping(value="/",method = RequestMethod.GET)
	@Transactional(propagation = Propagation.REQUIRED)
	public List<HouseOwnerInfo> getHouseOwnersInfo(@RequestParam("houseCode")String houseId,@RequestParam("ownerInfo")String ownerInfo) throws CommonException{
		return houseOwnerService.getHouseOwnersInfo(houseId,ownerInfo);
	}
}
